<template>
  <div class="detail-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      label-width="100px"
      label-suffix=":"
    >

      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.color">
        <el-button v-loading="loading" type="success" @click="submitForm">
          {{ isEdit ? '更新' : '保存' }}
        </el-button>
      </sticky>

      <div class="app-container" style="padding: 10px 20px">
        <warn />

        <el-row>
          <el-col :span="22">
            <el-form-item label="名称" prop="name" required>
              <el-input v-model.trim="postForm.name" />
            </el-form-item>
            <el-form-item label="链接" prop="url" required>
              <el-input v-model.trim="postForm.url" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import api from '@/api'
import Sticky from '@/components/Sticky'
import Warn from './Warn'
// import UploadImage from '@/components/Upload/UploadImage'
// import Tinymce from '@/components/Tinymce'
// import config from '@/config'
export default {
  // name: 'NewsDetail',
  components: {
    Sticky,
    Warn,
    // UploadImage,
    // Tinymce
  },
  props: {
    /**
     * 是否为编辑
     */
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const nameValidate = (rule, value, callback) => {
      if (value === undefined || value.length <= 0) {
        callback(new Error('请输入名称'))
      } else {
        callback()
      }
    }
    const urlValidate = (rule, value, callback) => {
      if (value === undefined || value.length <= 0) {
        callback(new Error('请输入链接'))
      } else {
        callback()
      }
    }
    // const sortValidate = (rule, value, callback) => {
    //   if (+value < -128 || +value > 127) {
    //     callback(new Error('排序值错误'))
    //   } else {
    //     callback()
    //   }
    // }
    // const imageValidate = (rule, value, callback) => {
    //   if (value.length === 0) {
    //     callback(new Error('请上传图片'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      id: -1,
      loading: false,
      postForm: {
        color: 'draft',
        // status: true,
        // sort: 0,
        // image: [],
        // view_virtual: 100
      },
      rules: {
        name: [{ validator: nameValidate, trigger: 'blur' }],
        url: [{ validator: urlValidate, trigger: 'blur' }],
        // sort: [{ validator: sortValidate, trigger: 'change' }]
        // image: [{ validator: imageValidate, trigger: 'change' }]
      },
      // categoryList: []
    }
  },
  created() {
    /**
     * 获取编辑数据
     */
    if (this.isEdit) {
      this.id = (this.$route.params && this.$route.params.id) || -1
      this.fetchData(this.id)
    }
    // this.getCategoryList()
  },
  methods: {
    // getCategoryList() {
    //   api.article.getCategoryList({ export: true, status: config.news.OPEN }).then(response => {
    //     this.categoryList = response.data
    //   })
    // },
    /**
     * 获取编辑数据
     */
    async fetchData(id) {
      const response = await api.roleMenu.view({ id })
      this.postForm = Object.assign({}, response.data)
      // // 状态
      // this.postForm.status = +this.postForm.status === 1
      // // 图片
      // if (this.postForm.image) {
      //   this.postForm.image = [{
      //     id: +this.postForm.image.id,
      //     url: this.postForm.image.thumbnail_path
      //   }]
      // } else {
      //   this.postForm.image = []
      // }
    },
    /**
     * 提交表单
     */
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          // // 状态
          // const status = this.postForm.status ? 1 : 0
          // // 图片
          // let image = 0
          // if (this.postForm.image && this.postForm.image.length > 0) {
          //   image = this.postForm.image.map(x => x.id)[0]
          // }
          api.roleMenu.edit({
            id: this.id,
            ...this.postForm,
            // status,
            // image_id: image
          }).then(response => {
            this.loading = false
            this.$notify({
              title: '成功',
              message: response.message || '操作成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push(`/permission/menu-edit/${response.data.id}`)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    // /**
    //  * 图片上传
    //  */
    // onSuccess(data, fileList) {
    //   if (data && data.id) {
    //     this.postForm.image.push({
    //       id: data.id,
    //       url: data.thumbnail
    //     })
    //   }
    // },
    // /**
    //  * 删除图片
    //  */
    // onRemove(data, fileList) {
    //   if (data && data.id) {
    //     this.postForm.image = this.postForm.image.filter(x => x.id !== data.id)
    //   }
    // }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  //
}
</style>
